package server;

import java.io.*;
import common.*;
import java.net.Socket;
import java.util.UUID;

import javax.swing.JTable;

public class Worker extends Thread{

    private Socket 			cs;
    private ServerThread 	st;
    private MyTextArea 		logs;
    private JTable 			table;
    private UUID 			myID;
    private JDBC 			db;
    
    public Worker(Socket 		cs, 
    			  ServerThread  st, 
    			  MyTextArea 	logs, 
    			  UUID 	   		myID, 
    			  JTable 		table,
    			  JDBC 			db)
    {
        this.cs = cs;
        this.st = st;
        this.logs = logs;
        this.myID = myID;
        this.table = table;
        this.db = db;
    }
    
    public void SendTable()
    {
    	try{
    		SendTableModel stm = new SendTableModel();
            stm.tableModel = db.getTableModel();
            OutputStream os = cs.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(stm);
            oos.flush();
        }catch (IOException e){
            logs.Append(e.getMessage());
        }
    }
    
    public void SendOutputFile(byte[] bytes)
    {
    	try{
    		SendOutputFile sof = new SendOutputFile();
    		sof.outputFile = bytes;
            OutputStream os = cs.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(sof);
            oos.flush();
        }catch (IOException e){
            logs.Append(e.getMessage());
        }    
    }
    
    @Override
    public void run() {
        try{
            InputStream is = cs.getInputStream();
            OutputStream os = cs.getOutputStream();
            int nComand;
            while(true)
            {
                nComand = is.read();
                logs.Append("Getcommand: " + Integer.toString(nComand));
                // Request UUID
                if(nComand == 1){
                    SendUUID su = new common.SendUUID();
                    su.idUser = myID; 
                    ObjectOutputStream oos = new ObjectOutputStream(os);
                    oos.writeObject(su);
                    oos.flush();
                    logs.Append("Send UUID: " + myID.toString());
                }
                // Unlink Client
                if(nComand == 2){
                    st.unlink(myID);
                    logs.Append("Client unlink");
                    break;
                }
                // Client Request: Upload task
                if(nComand == 3){
                	
                	// Receive Object
                	SendBin obj = null;
                    try {
            	        ObjectInputStream oos = new ObjectInputStream(is);
            	        obj = (SendBin) oos.readObject();

            		} catch (IOException e) {
            			e.printStackTrace();
            			return;
            		}
                	db.LoadTableToDataBase(obj);
                }
                
                // Client Request: Run task
                if(nComand == 4){
                	     
                	// Receive Object
                	SendRunRequest obj = null;
                    try {
            	        ObjectInputStream oos = new ObjectInputStream(is);
            	        obj = (SendRunRequest) oos.readObject();
            		} catch (IOException e) {
            			e.printStackTrace();
            			return;
            		}
	                db.RunTask(obj.fileName,obj.version);	                
                }
                
                // Client Request: Download result
                if(nComand == 5){
                	// Receive Object
                	SendDownloadRequest obj = null;
                    try {
            	        ObjectInputStream oos = new ObjectInputStream(is);
            	        obj = (SendDownloadRequest) oos.readObject();
            		} catch (IOException e) {
            			e.printStackTrace();
            			return;
            		}
                    
                    SendOutputFile(db.getOutputFile(obj.fileName,obj.version));	
	                  
                }
                
                // Client Request: Send table
                if(nComand == 6){
                	SendTable();
                }

            }
            cs.close();
        } catch (IOException e){
            logs.Append(e.getMessage());
        }
        catch (Exception e){
            logs.Append(e.getMessage());
        }
    }
}


